package com.study.servlet;

import com.study.pojo.User;
import com.study.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "SignInServlet")
public class SignInServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String username = request.getParameter("username");
       String password = request.getParameter("password");
       String sql = "select * from tb_users where username=? and password=?";
       Object[] params = {username,password};
       QueryRunner qr = new QueryRunner(DruidUtils.getDataSources());
        try {
            User user = qr.query(sql, new BeanHandler<User>(User.class), params);
            if(user == null){
                System.out.println("fail");
                response.getWriter().print("fail");
            }else{
                System.out.println("success");
                response.getWriter().print("success");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
